package com.vogue.supplymall.order.domain.service.impl;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.match.domain.dto.OrderPushDetailDTO;
import com.vogue.supplymall.match.domain.dto.PushBillInfoDTO;
import com.vogue.supplymall.order.domain.dao.OrderItemDao;
import com.vogue.supplymall.order.domain.model.OrderItem;
import com.vogue.supplymall.order.domain.service.OrderItemService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;

/**
 * The type Order item service.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-01-10
 */
@Service("orderItemService")
public class OrderItemServiceImpl extends BaseServiceImpl<OrderItem, Long> implements OrderItemService {

    @Resource
    private OrderItemDao orderItemDao;

    /**
     * Instantiates a new Order item service.
     *
     * @param baseDao the base dao
     */
    public OrderItemServiceImpl(BaseDao<OrderItem, Long> baseDao) {
        super(baseDao);
    }

    @Override
    public OrderItem addOrderItem(OrderItem orderItem) {
        return orderItemDao.save(orderItem);
    }

    @Override
    public Long getOrderItemCount(OrderItem orderItem) {
        return orderItemDao.getOrderItemCount(orderItem.getOrderid(), OrderItem.class);
    }

	@Override
	public void changSendInfo(List<PushBillInfoDTO> pbidList)  throws Exception{
		for(PushBillInfoDTO pbiDTO:pbidList) {
			//String orderId=pbiDTO.getOrderid();
			List<OrderPushDetailDTO> opddList=pbiDTO.getDetails();
			
			for(OrderPushDetailDTO opdDTO:opddList) {
				BigDecimal sendCount=opdDTO.getSendCount();
				BigDecimal sendMoney=opdDTO.getSendMoney();
				BigDecimal diffCount=opdDTO.getDiffCount();
				BigDecimal diffMoney=opdDTO.getDiffMoney();
				BigInteger detailId=BigInteger.ZERO;
				String supplyId=opdDTO.getSupplyId();
				if(StringUtils.isNotBlank(supplyId)) {
					detailId=new BigInteger(opdDTO.getSupplyId());					
				}else {
					continue;
				}
				
				OrderItem oi=orderItemDao.get(detailId);
				oi.setSendcount(sendCount);
				oi.setSendmoney(sendMoney);
				oi.setDiffcount(diffCount);
				oi.setDiffmoney(diffMoney);
				
				orderItemDao.update(oi);
				
			}
			
		}
		
	}
    
	
}
